<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Models\DecSubject;
use Illuminate\Support\Arr;use Illuminate\Support\Facades\DB;
use App\Common\Utils\JsonResponse;
use App\Models\DictionaryItem;
use Illuminate\Http\Request;

use function GuzzleHttp\json_decode;

class DictionaryItemController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }


    /**
     * 获取字典数据
     * @author lxy
     * @datetime 2023-02-16
     */
    public function getDictionaryItem(Request $Request)
    {
        $request = $Request->all();
        $dicCode = $request['diccode'];
        if(!$dicCode){
            return [
                'code'=>1,
                'msg'=>'',
                'data'=>[]
            ];
        }
        $dics = [];

        $decSubject = DecSubject::query()->select('id as optionId','name as selectValue','name as selectKey')
            ->get()->map(function ($item){
                $item->parentId = 0;
                $item->order = 0;
                return $item;
            });
        if (in_array('SUBJECT',$dicCode)){
            $dics['SUBJECT'] = $decSubject->toArray();
            $dicCode = array_diff($dicCode,['SUBJECT']);
        }
        if (in_array('CLASS',$dicCode) || in_array('class',$dicCode)){
            $dics['CLASS'] = [];
            $classP = DictionaryItem::select('id')->where('code','GRADE')->where(['parent_id'=>0])->orderBy('id','desc')->first();
            if($classP) {
                $dics['CLASS'] = DictionaryItem::select('code', 'item_order as order', 'id as optionId', 'parent_id as parentId', 'item_value as selectKey', 'item_name as selectValue')
                    ->where('code', 'CLASS')
                    ->where('parent_id', $classP->id)
                    ->where('deleted', 0)
                    ->orderBy('item_order')
                    ->get()->toArray();
                $dicCode = array_diff($dicCode, ['CLASS','class']);
            }
        }
        if (!empty($dicCode)) {
            $dic = DictionaryItem::select('code', 'item_order as order', 'id as optionId', 'parent_id as parentId', 'item_value as selectKey', 'item_name as selectValue')
                ->whereIn('code', $dicCode)
                ->where('deleted', 0)
                ->orderBy('item_order')
                ->get()->toArray();
            foreach ($dic as $k => $v) {
                $dics[$v['code']][] = $v;
            }
        }

        return [
            'code'=>1,
            'msg'=>'',
            'data'=>$dics
        ];
    }

    /**
     * 获取字典数据
     * @author kzj
     * @datetime 2023-04-20
     */
    public function getDictionaryLinkItem(Request $Request)
    {
        $request = $Request->all();
        if(empty($request['parent_id'])){
            return [
                'code'=>1,
                'msg'=>'',
                'data'=>[]
            ];
        }
        $dic = DictionaryItem::select('code','item_order as order','id as optionId','parent_id as parentId','item_value as selectKey','item_name as selectValue')
            ->where('parent_id',$request['parent_id'])->where('deleted',0)->orderBy('id')->get()->toArray();
        $dics = [];
        foreach($dic as $k=>$v){
            $dics[$v['code']][] = $v;
        }
        return [
            'code'=>1,
            'msg'=>'',
            'data'=>$dics
        ];
    }

    /**
     * @Description: 获取籍贯下拉框数据
     * @Author: kzj
     * @datetime: 2023-03-09
     */
    public function getOriginSelect(Request $request)
    {
        $dics = DictionaryItem::select('id', 'code', 'item_name', 'parent_id')
            ->where('code', 'province')
            ->with('children')
            ->get()
            ->toArray();

        $result = [
            'code' => 1,
            'msg' => '',
            'data'=> $dics
        ];

        return $result;
    }

    /**
     * @Description: 获取籍贯下拉框数据
     * @Author: kzj
     * @datetime: 2023-03-09
     */
    public function getDicRelationItem(Request $request)
    {
        try{
            $requestData = $request->all();

            if(!$requestData['pid']) {
                throw new \Exception('参数缺失！');
            }

            $dics = DictionaryItem::where('parent_id', $requestData['pid'])
                ->when(isset($requestData['code']) && !empty($requestData['code']), function($q) use($requestData) {
                    $q->where('code', $requestData['code']);
                })
                ->get()
                ->toArray();

            $result = [
                'code' => 1,
                'msg' => '',
                'data'=> $dics
            ];

            return $result;
        }catch(\Exception $e){
            return JsonResponse::error($e->getMessage());
        }

    }


    /**
     * @Description: 获取老字典多选下拉数据
     * @Author: kzj
     * @datetime: 2023-05-26
     */
    public function getDictionaryXmSelect(Request $request)
    {
        $requestData = $request->all();
        $dicCode = $request['diccode'];
        if(empty($request['diccode'])){
            return [
                'code'=>1,
                'msg'=>'',
                'data'=>[]
            ];
        }
        //select('id as optionId','name as selectValue','name as selectKey')
        $decSubject = DecSubject::query()->select('id as code','id as value','name')
            ->get()->map(function ($item){
                //$item->parentId = 0;
                $item->order = 0;
                $item->code = 'SUBJECT';
                return $item;
            });
        $dics = [];
        if (in_array('SUBJECT',$dicCode)){
            $dics['SUBJECT'] = $decSubject->toArray();
            $dicCode = array_diff($dicCode,['SUBJECT']);
        }
        if (!empty($dicCode)) {
            $dic = DictionaryItem::select('code', 'item_order as order', 'id as value', 'item_name as name')
                ->whereIn('code', $dicCode)
                ->where('deleted', 0)
                ->orderBy('item_order')
                ->get()
                ->toArray();
            foreach ($dic as $k => $v) {
                $dics[$v['code']][] = $v;
            }
        }
        return [
            'code'=>1,
            'msg'=>'',
            'data'=>$dics
        ];
    }
}


?>
